﻿using System;
using System.Collections.Generic;
using System.Text;

namespace MikeKappel.Com.CS
{
    public static partial class Helper
    {
        /// <summary>
        /// Concatenate a member of a Generic List
        /// </summary>
        /// <example>
        /// String HostsAsCSV = Helper.Concatenate<Uri>(",", new List<Uri>(), delegate(Uri item) { return item.Host; });
        /// </example>
        /// <typeparam name="T">Type</typeparam>
        /// <param name="delimiter">Delimiter</param>
        /// <param name="items">Generic List</param>
        /// <param name="converter">delegate</param>
        /// <returns>Delimited String</returns>
        public static string Concatenate<T>(string delimiter
                             , List<T> items
                             , Converter<T, string> converter)
        {
            StringBuilder builder = new StringBuilder();
            items.ForEach(delegate(T item)
            {
                if (builder.Length > 0)
                    builder.Append(delimiter);
                builder.Append(converter(item));
            });
            return builder.ToString();
        }

        /// <summary>
        /// Concatenate a member of a Generic List
        /// </summary>
        /// <typeparam name="T">Type</typeparam>
        /// <param name="padding">Amount of Padding</param>
        /// <param name="items">Generic List</param>
        /// <param name="converter">delegate</param>
        /// <returns>Delimited String</returns>
        public static string Concatenate<T>(Int32 padding
                             , List<T> items
                             , Converter<T, string> converter)
        {
            StringBuilder builder = new StringBuilder();
            items.ForEach(delegate(T item)
            {
                builder.Append(converter(item).PadRight(padding));
            });
            return builder.ToString();
        }
    }
}